# Contributor: Peter Shkenev <santurysim@gmail.com>
# Maintainer: Peter Shkenev <santurysim@gmail.com>
pkgname=trafficserver9
_pkgreal=trafficserver
pkgver=9.2.3
pkgrel=0
provides="trafficserver=$pkgver-r$pkgrel"
pkgdesc="Fast, scalable and extensible HTTP/1.1 and HTTP/2 compliant caching proxy server"
url="https://trafficserver.apache.org/"
# other platforms are unsupported
# ppc64le: luajit
arch="x86_64 aarch64"
license="Apache-2.0"
makedepends="
	curl-dev
	flex
	hwloc-dev
	libcap-dev
	libtool
	libunwind-dev
	luajit-dev
	ncurses-dev
	openssl-dev>3
	pcre-dev
	perl
	zlib-dev
	"
options="!check"	# test suite fails, reported to upstream
subpackages="
	$pkgname-doc
	$pkgname-dev
	perl-$pkgname:perl_ts:noarch
	$pkgname-openrc
	$pkgname-utils
	$pkgname-plugin-base:plugin_base
	$pkgname-plugin-authproxy:plugin_authproxy
	$pkgname-plugin-backgroundfetch:plugin_backgroundfetch
	$pkgname-plugin-s3auth:plugin_s3auth
	$pkgname-plugin-cachekey:plugin_cachekey
	$pkgname-plugin-cachepromote:plugin_cachepromote
	$pkgname-plugin-cacherangerequests:plugin_cacherangerequests
	$pkgname-plugin-combohandler:plugin_combohandler
	$pkgname-plugin-confremap:plugin_confremap
	$pkgname-plugin-esi:plugin_esi
	$pkgname-plugin-escalate:plugin_escalate
	$pkgname-plugin-generator:plugin_generator
	$pkgname-plugin-compress:plugin_compress
	$pkgname-plugin-headerrewrite:plugin_headerrewrite
	$pkgname-plugin-healthchecks:plugin_healthchecks
	$pkgname-plugin-lua:plugin_lua
	$pkgname-plugin-regexremap:plugin_regexremap
	$pkgname-plugin-regexrevalidate:plugin_regexrevalidate
	$pkgname-plugin-statsoverhttp:plugin_statsoverhttp
	$pkgname-plugin-tcpinfo:plugin_tcpinfo
	$pkgname-plugin-xdebug:plugin_xdebug
	$pkgname-plugin-certifier:plugin_certifier
	$pkgname-plugin-multiplexer:plugin_multiplexer
	$pkgname-plugin-prefetch:plugin_prefetch
	$pkgname-plugin-remappurge:plugin_remappurge
	"
source="
	https://dlcdn.apache.org/trafficserver/trafficserver-$pkgver.tar.bz2
	10-fix-PATH_MAX-on-ppc64le.patch
	20-adapt-trafficserver.patch
	30-fix-perl-installation.patch
	40-alpine-dir-layout.patch
	no-execinfo.patch
	fix-pthread.patch
	trafficserver.initd
	"
install="$pkgname.pre-install $pkgname.post-install"
builddir="$srcdir/$_pkgreal-$pkgver"
pkgusers="trafficserver"
pkggroups="trafficserver"

# secfixes:
#   9.2.3-r0:
#     - CVE-2023-39456
#     - CVE-2023-41752
#     - CVE-2023-44487

build() {
	# Layout contains all dir variables
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--enable-layout=Alpine \
		--with-user=trafficserver \
		--with-group=trafficserver
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	find "$pkgdir" \( -name perllocal.pod -o -name .packlist \) -delete
	install -Dm755 "$srcdir"/trafficserver.initd "$pkgdir"/etc/init.d/trafficserver
}

perl_ts() {
	pkgdesc="Perl modules for managing Apache Traffic Server"
	depends="perl $depends"
	amove usr/lib/perl5
	amove usr/share/perl5
}

plugin_base() {
	pkgdesc="Apache Traffic Server plugin core libraries"
	amove usr/lib/plugin*
	amove usr/libexec/trafficserver/libloader.so
	amove usr/libexec/trafficserver/server_push_preload.so
}

plugin_authproxy() {
	pkgdesc="Apache Traffic Server AuthProxy plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/authproxy.so
}

plugin_backgroundfetch() {
	pkgdesc="Apache Traffic Server Background Fetch plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/background_fetch.so
}

plugin_s3auth() {
	pkgdesc="Apache Traffic Server AWS S3 Authentication plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/s3_auth.so
}

plugin_cachekey() {
	pkgdesc="Apache Traffic Server Cache Key and Parent Selection URL Manipulation plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/cachekey.so
}

plugin_cachepromote() {
	pkgdesc="Apache Traffic Server Cache Promotion Policies plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/cache_promote.so
}

plugin_cacherangerequests() {
	pkgdesc="Apache Traffic Server Cache Range Requests plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/cache_range_requests.so
}

plugin_combohandler() {
	pkgdesc="Apache Traffic Server Combo Handler plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/combo_handler.so
}

plugin_confremap() {
	pkgdesc="Apache Traffic Server Configuration Remap plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/conf_remap.so
}

plugin_esi() {
	pkgdesc="Apache Traffic Server ESI plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/esi.so
}

plugin_escalate() {
	pkgdesc="Apache Traffic Server Escalate plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/escalate.so
}

plugin_generator() {
	pkgdesc="Apache Traffic Server Generator plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/generator.so
}

plugin_compress() {
	pkgdesc="Apache Traffic Server Compress plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/compress.so
}

plugin_headerrewrite() {
	pkgdesc="Apache Traffic Server Header Rewrite plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/header_rewrite.so
}

plugin_healthchecks() {
	pkgdesc="Apache Traffic Server Health Checks plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/healthchecks.so
}

plugin_lua() {
	pkgdesc="Apache Traffic Server Lua plugin"
	depends="$pkgname-plugin-base luajit $depends"
	amove usr/libexec/trafficserver/tslua.so
}

plugin_regexremap() {
	pkgdesc="Apache Traffic Server Regex Remap plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/regex_remap.so
}

plugin_regexrevalidate() {
	pkgdesc="Apache Traffic Server Regex Revalidate plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/regex_revalidate.so
}

plugin_statsoverhttp() {
	pkgdesc="Apache Traffic Server Stats over HTTP plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/stats_over_http.so
}

plugin_tcpinfo() {
	pkgdesc="Apache Traffic Server TPCinfo plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/tcpinfo.so
}

plugin_xdebug() {
	pkgdesc="Apache Traffic Server XDebug plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/xdebug.so
}

plugin_certifier() {
	pkgdesc="Apache Traffic Server Certifier plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/certifier.so
}

plugin_multiplexer() {
	pkgdesc="Apache Traffic Server Multiplexer plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/multiplexer.so
}

plugin_prefetch() {
	pkgdesc="Apache Traffic Server Prefetch plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/prefetch.so
}

plugin_remappurge() {
	pkgdesc="Apache Traffic Server Remap Purge plugin"
	depends="$pkgname-plugin-base $depends"
	amove usr/libexec/trafficserver/remap_purge.so
}

utils() {
	pkgdesc="Apache Traffic Server auxiliary utils"
	depends="curl $depends"
	amove usr/bin/traffic_cache_tool
	amove usr/bin/traffic_crashlog
	amove usr/bin/traffic_layout
	amove usr/bin/traffic_logcat
	amove usr/bin/traffic_logstats
	amove usr/bin/traffic_top
	amove usr/bin/traffic_via
	amove usr/bin/tspush
	amove usr/bin/tsxs
}

sha512sums="
acb511873a051c2cdfddccabf420be79cba01ae241470738658a89bcf636074bf41cda62552e01f41b9549295f817c566a1696aec057509a8c93f3b22ae9e90e  trafficserver-9.2.3.tar.bz2
fbbe4fd4024b4656a3d4f67bdace7ae3a65892d40a92c67245e277520dfd53d5e9eb7934b2d213dfecc4de6895c63ec67cef0edee7b43ba28319cfaa3c75ddf3  10-fix-PATH_MAX-on-ppc64le.patch
fe9b8344979fc5da885203146f4052016401354aa5310c30edc0e89f714e47492b1ec900e5f8ca02bd1910f0d4b4f4512536d9e7db9cf87f127e7259aba315dd  20-adapt-trafficserver.patch
47d7d83be1e7798bb905c217efab9480aa6e6218eb09317c20c457f454895016a6104818d86223a051c8adc40f42c7436aa607f73e505c939bd2d5dd8e5efa45  30-fix-perl-installation.patch
791b5ea9620574b172587fb2fcde97da53ca4c4a64110f1e1e13b9e475ffa255753fd98933a2f0f1ef44a2bf7810f6828445147fc6dcc8a41869b62271a728ad  40-alpine-dir-layout.patch
53a834590ae5e4487229895f60fd1e34858418419655e37c03f8c4a1075905b40449dde817f10b39173ac27391ec9a6e75839bb5a4d8f631d8fb9d479d0a8d15  no-execinfo.patch
b675d57791d427a75dce63b35574bbf49192451cf022c5332c042d0e578c0d89ceff4c4c79ac71e323710c935c2735ceb0e0ebcd12709c5e67be44dce0c9d7fb  fix-pthread.patch
a4d2e91d92579ba8b0c5361b0f4fd490d7a90e8a170552428f4781432eca89503a87a2c6f571a342eb77db5a43baa03d779a1fbe37ce1f4bba24fe89d5c97e7d  trafficserver.initd
"
